package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.SupplierFile;
import net.hn.hnms.biz.project.domain.SupplierInfo;
import net.hn.hnms.biz.project.mapper.SupplierFileMapper;
import net.hn.hnms.biz.project.mapper.SupplierInfoMapper;
import net.hn.hnms.biz.project.request.SupplierInfoReq;
import net.hn.hnms.biz.project.service.SupplierInfoService;
import net.hn.hnms.biz.project.vo.SupplierInfoVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SupplierInfoServiceImpl implements SupplierInfoService {
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private SupplierFileMapper supplierFileMapper;



    @Override
    @Transactional
    public int insert(SupplierInfoReq supplierInfoReq) {
        SupplierInfo insertObj = new SupplierInfo();
        BeanUtil.copyProperties(supplierInfoReq, insertObj);
        int flag = supplierInfoMapper.insert(insertObj);
        List<SupplierFile> supplierFileList = supplierInfoReq.getSupplierFile();
        if(supplierFileList != null && !supplierFileList.isEmpty()){
            for(SupplierFile file:supplierFileList){
                file.setSupplierId(insertObj.getSupplierId());
                supplierFileMapper.insert(file);
            }
        }
        return flag;
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        supplierInfoMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(SupplierInfoReq supplierInfoReq) {
        SupplierInfo updateObj = new SupplierInfo();
        BeanUtil.copyProperties(supplierInfoReq, updateObj);
        int updateCount = supplierInfoMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        supplierFileMapper.updateDeleteById(supplierInfoReq.getSupplierId());
        List<SupplierFile> supplierFileList = supplierInfoReq.getSupplierFile();
        if(supplierFileList != null && !supplierFileList.isEmpty()) {
            for (SupplierFile file : supplierFileList) {
                file.setSupplierId(supplierInfoReq.getSupplierId());
                file.setSupplierFileId(null);
                supplierFileMapper.insert(file);
            }
        }
    }

    @Override
    public SupplierInfoVo selectDetailById(String id) {
        SupplierInfoVo vo = new SupplierInfoVo();
        SupplierInfo supplierInfo = supplierInfoMapper.selectById(id);
        if (supplierInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        BeanUtil.copyProperties(supplierInfo, vo);
        List<SupplierFile> SupplierFile = supplierFileMapper.selectFileById(id);
        vo.setSupplierFile(SupplierFile);
        return vo;
    }

    @Override
    public ResponsePage<SupplierInfo> selectPageList(SupplierInfoReq supplierInfoReq, PageQuery pageQuery) {
        Page<SupplierInfo> page = supplierInfoMapper.selectPage(pageQuery.build(), buildQuery(supplierInfoReq).orderByDesc(SupplierInfo::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<SupplierInfo> buildQuery(SupplierInfoReq req){
        return new LambdaQueryWrapper<SupplierInfo>()
                .eq(StrUtil.isNotBlank(req.getMineCode()), SupplierInfo::getMineCode, req.getMineCode())
                .like(StrUtil.isNotBlank(req.getSupplierName()), SupplierInfo::getSupplierName, req.getSupplierName())
                .like(StrUtil.isNotBlank(req.getExecutorUserName()), SupplierInfo::getSupplierName, req.getExecutorUserName())
                .like(StrUtil.isNotBlank(req.getSupplierAbbreviation()), SupplierInfo::getSupplierAbbreviation, req.getSupplierAbbreviation())
                .like(StrUtil.isNotBlank(req.getLegalPersonName()), SupplierInfo::getLegalPersonName, req.getLegalPersonName());
    }
}
